home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / gs24src.zip / ZDEVICE.C < prev    next >
C/C++ Source or Header  |  1992-02-28  |  5KB  |  197 lines

  1. /* Copyright (C) 1989, 1992 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* zdevice.c */
  21. /* Device-related operators for GhostScript */
  22. #include "ghost.h"
  23. #include "alloc.h"
  24. #include "errors.h"
  25. #include "oper.h"
  26. #include "state.h"
  27. #include "gsmatrix.h"
  28. #include "gsstate.h"
  29. #include "gxdevice.h"
  30. #include "store.h"
  31.  
  32. /* copy for devices -- called from zcopy in zgeneric.c. */
  33. int
  34. zcopy_device(register os_ptr op)
  35. {    gx_device *new_dev;
  36.     int code = gs_copydevice(&new_dev, op->value.pdevice, alloc);
  37.     if ( code < 0 ) return code;
  38.     make_tv(op, t_device, pdevice, new_dev);
  39.     return 0;
  40. }
  41.  
  42. /* copyscanlines */
  43. int
  44. zcopyscanlines(register os_ptr op)
  45. {    os_ptr op1 = op - 1;
  46.     os_ptr op2 = op - 2;
  47.     gx_device *dev;
  48.     int code;
  49.     uint bytes_copied;
  50.     check_type(*op2, t_device);
  51.     dev = op2->value.pdevice;
  52.     check_type(*op1, t_integer);
  53.     if ( op1->value.intval < 0 || op1->value.intval > dev->height )
  54.         return e_rangecheck;
  55.     check_write_type(*op, t_string);
  56.     code = gs_copyscanlines(dev, (int)op1->value.intval,
  57.         op->value.bytes, r_size(op), NULL, &bytes_copied);
  58.     if ( code < 0 ) return e_typecheck;    /* not a memory device */
  59.     *op2 = *op;
  60.     r_set_size(op2, bytes_copied);
  61.     pop(2);
  62.     return 0;
  63. }
  64.  
  65. /* currentdevice */
  66. int
  67. zcurrentdevice(register os_ptr op)
  68. {    gx_device *dev = gs_currentdevice(igs);
  69.     push(1);
  70.     make_tv(op, t_device, pdevice, dev);
  71.     return 0;
  72. }
  73.  
  74. /* devicename */
  75. int
  76. zdevicename(register os_ptr op)
  77. {    char *dname;
  78.     int code;
  79.     check_type(*op, t_device);
  80.     dname = gs_devicename(op->value.pdevice);
  81.     code = string_to_ref(dname, op, "devicename");
  82.     if ( code < 0 ) return code;
  83.     return 0;
  84. }
  85.  
  86. /* deviceinitialmatrix */
  87. int
  88. zdeviceinitialmatrix(register os_ptr op)
  89. {    int code = write_matrix(op);
  90.     if ( code < 0 ) return code;
  91.     check_type(op[-1], t_device);
  92.     gs_deviceinitialmatrix(op[-1].value.pdevice, (gs_matrix *)op->value.refs);
  93.     op[-1] = *op;
  94.     pop(1);
  95.     return 0;
  96. }
  97.  
  98. /* flushpage */
  99. int
  100. zflushpage(register os_ptr op)
  101. {    return gs_flushpage(igs);
  102. }
  103.  
  104. /* getdevice */
  105. int
  106. zgetdevice(register os_ptr op)
  107. {    gx_device *dev;
  108.     check_type(*op, t_integer);
  109.     if ( op->value.intval != (int)(op->value.intval) )
  110.         return e_rangecheck;    /* won't fit in an int */
  111.     dev = gs_getdevice((int)(op->value.intval));
  112.     if ( dev == 0 ) return e_rangecheck;    /* index out of range */
  113.     make_tv(op, t_device, pdevice, dev);
  114.     return 0;
  115. }
  116.  
  117. /* makeimagedevice */
  118. int
  119. zmakeimagedevice(register os_ptr op)
  120. {    gs_matrix imat;
  121.     gx_device *new_dev;
  122.     byte *colors;
  123.     int num_colors;
  124.     int code;
  125.     check_type(op[-2], t_integer);    /* width */
  126.     check_type(op[-1], t_integer);    /* height */
  127.     if ( r_has_type(op, t_null) )    /* true color */
  128.        {    colors = 0;
  129.         num_colors = -24;    /* 24-bit true color */
  130.        }
  131.     else
  132.        {    check_type(*op, t_string);    /* palette */
  133.         if ( r_size(op) > 3*256 ) return e_rangecheck;
  134.         colors = op->value.bytes;
  135.         num_colors = r_size(op);
  136.        }
  137.     if (    (ulong)(op[-2].value.intval) > max_uint >> 1 ||
  138.         (ulong)(op[-1].value.intval) > max_uint >> 1
  139.        ) return e_rangecheck;
  140.     if ( (code = read_matrix(op - 3, &imat)) < 0 ) return code;
  141.     /* Everything OK, create device */
  142.     code = gs_makeimagedevice(&new_dev, &imat,
  143.                   (int)op[-2].value.intval,
  144.                   (int)op[-1].value.intval,
  145.                   colors, num_colors, alloc);
  146.     if ( code == 0 )
  147.        {    make_tv(op - 3, t_device, pdevice, new_dev);
  148.         pop(3);
  149.        }
  150.     return code;
  151. }
  152.  
  153. /* nulldevice */
  154. int
  155. znulldevice(register os_ptr op)
  156. {    gs_nulldevice(igs);
  157.     return 0;
  158. }
  159.  
  160. /* .outputpage */
  161. int
  162. zoutputpage(register os_ptr op)
  163. {    int code;
  164.     check_type(op[-1], t_integer);
  165.     check_type(*op, t_boolean);
  166.     code = gs_output_page(igs, (int)op[-1].value.intval, op->value.index);
  167.     if ( code < 0 ) return code;
  168.     pop(2);
  169.     return 0;
  170. }
  171.  
  172. /* setdevice */
  173. int
  174. zsetdevice(register os_ptr op)
  175. {    int code;
  176.     check_type(*op, t_device);
  177.     code = gs_setdevice(igs, op->value.pdevice);
  178.     if ( code == 0 ) pop(1);
  179.     return code;
  180. }
  181.  
  182. /* ------ Initialization procedure ------ */
  183.  
  184. op_def zdevice_op_defs[] = {
  185.     {"3copyscanlines", zcopyscanlines},
  186.     {"0currentdevice", zcurrentdevice},
  187.     {"2deviceinitialmatrix", zdeviceinitialmatrix},
  188.     {"1devicename", zdevicename},
  189.     {"0flushpage", zflushpage},
  190.     {"1getdevice", zgetdevice},
  191.     {"4makeimagedevice", zmakeimagedevice},
  192.     {"0nulldevice", znulldevice},
  193.     {"2.outputpage", zoutputpage},
  194.     {"1setdevice", zsetdevice},
  195.     op_def_end(0)
  196. };
  197.